﻿using System;
using System.Collections.Generic;
using System.Web;
using WeiboFilter.Models;
using WeiboFilter.DAL;
using WeiboFilter.KeywordsRelated;
namespace WeiboFilter.BLL
{
    public class StatusBLL
    {
        /// <summary>
        /// 得到打分后的微博信息
        /// </summary>
        /// <param name="n"></param>
        /// <param name="customerId"></param>
        /// <returns></returns>
        public IList<Status> GetTopStatusWithScore(int n, string customerId)
        {
            StatusDAL sD = new StatusDAL();
            WordsDAL wD = new WordsDAL(customerId);

            //得到未打分的n条微博
            List<Status> sL = sD.GetTopWeiBoStatuses(n, customerId);

            //对每条微博打分
            foreach (Status s in sL)
            {
                s.state = Stat.Dirty;
                KeyWords kkk = new KeyWords(s.text);
                //当没能提取关键字，即微博完全没意义，没有n,v,a，直接过滤
                if (kkk.words.Count == 0)
                {
                    s.point = -100;
                }
                else
                {
                    s.point = 0;
                    foreach (WordWeight kw in kkk.words)
                    {
                        Word w = wD.GetWordInfo(kw.word);
                        if (w != null)//空的话，就是0分
                            s.point += w.point * kw.weight / 100;
                    }
                }
                if (s.point > SystemConstant.threshold["guessLike"])
                {
                    s.state = Stat.GuessYouLike;
                }
                else if (s.point < SystemConstant.threshold["guessHate"])
                {
                    s.state = Stat.AutoFilter;
                }

                //把状态和分数更新回数据库中
                sD.UpdateWeiBoStatusState(s.id, (int)s.state, customerId, s.point);
            }

            return sL;
        }
    }
}